package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.Constant;
import com.mall.common.Result;
import com.mall.exception.MallException;
import com.mall.exception.MallExceptionEnum;
import com.mall.model.dto.AddProductRequest;
import com.mall.model.dto.ProductListRequest;
import com.mall.model.dto.UpdateProductRequest;
import com.mall.model.po.Product;
import com.mall.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "添加产品操作")
    @PostMapping("/admin/product/add")
    public Result addProduct(@RequestBody @Valid AddProductRequest addProductRequest) {
        Product product = new Product();
        BeanUtils.copyProperties(addProductRequest, product);

        productService.addProduct(product);
        return Result.success();
    }

    @ApiOperation("上传图片")
    @PostMapping("/admin/upload/file")
    public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws URISyntaxException, IOException {
//        获取上传图片的名字
        String oldName = file.getOriginalFilename();

//       切割文件名字，获取文件的后缀名
        String ext = oldName.substring(oldName.lastIndexOf("."));

//        生成文件的新名字
        String newName = UUID.randomUUID().toString() + ext;
//        创建文件
        File fileDir = new File(Constant.UPLOAD_DIR);

//        如果文件不存在
        if (!fileDir.exists()) {
            throw new MallException(MallExceptionEnum.NOT_EXIST_DIR);
        }

//        获取目标文件
        File destFile = new File(Constant.UPLOAD_DIR, newName);
//       上传文件
        file.transferTo(destFile);
//        获取ip的字符串
        URI uri = getImgURl(new URI(request.getRequestURL().toString()));
//        拼接出图片的访问路径
        String imageURL = uri+"/images/"+newName;
        System.out.println(imageURL);
//        返回图片路径地址
        return Result.success(imageURL);

    }
    private URI getImgURl(URI uri) throws URISyntaxException {
//        获取协议，ip 端口号
        URI result = new URI(uri.getScheme(),null,uri.getHost(),uri.getPort(),null,null,null);
        return result;
    }

    @ApiOperation(value="更改商品操作")
    @PostMapping("/admin/product/update")
    public Result updateProduct(@RequestBody @Valid UpdateProductRequest updateProductRequest){
    Product product = new Product();
    BeanUtils.copyProperties(updateProductRequest,product);
    productService.updateProduct(product);
    return Result.success();
}

    @ApiOperation("删除商品操作")
    @PostMapping("/admin/product/delete")
    public Result deleteProduct(@RequestParam("id") Integer id){
        productService.deleteProduct(id);
        return Result.success();
}

    @ApiOperation(value = "商品批量上下架")
    @PostMapping("/admin/product/batchUpdateSellStatus")
    public Result batchUpdateSellStatus(@RequestParam( "ids") Integer[] ids,
                                        @RequestParam("sellStatus") Integer sellStatus){
        productService.updateBatchSellStatus(ids,sellStatus);
        return Result.success();
    }

    @ApiOperation(value = "商品后台列表")
    @GetMapping("/admin/product/list")
    public Result list (@RequestParam("pageNum" )Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        PageInfo pageInfo = productService.listForAdmin(pageNum,pageSize);
        return Result.success(pageInfo);
    }
//
    @ApiOperation("前台商品详情")
    @GetMapping("/product/detail")
    public Result detail(@RequestParam("id") Integer id){
        Product product = productService.detail(id);
        return Result.success(product);
    }

    @ApiOperation("前台商品列表")
    @GetMapping("/product/list")
    public Result listForCustomer(ProductListRequest productListRequest){

        PageInfo pageInfo = productService.listForCustomer(productListRequest);
        return Result.success(pageInfo);

    }

}
